package pers.jsan.litheopen.security.oauth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import pers.jsan.litheopen.security.oauth.model.UserDetail;

public class UserContext {

    protected Object userDetail;

    public UserDetail getUserDetail() {
        if(null == this.userDetail) {
            return null;
        }
        JSONObject json = JSONObject.parseObject(JSON.toJSONString(this.userDetail));
        return this.userDetail instanceof UserDetail ? (UserDetail) this.userDetail : json.toJavaObject(UserDetail.class);
    }

    public void setUserDetail(Object userInfo) {
        this.userDetail = userInfo;
    }

    public Object getUserDetailForCustom(Class clz) {
        if(null == this.userDetail) {
            return null;
        }
        return this.userDetail instanceof JSONObject ? ((JSONObject) this.userDetail).toJavaObject(clz) : this.userDetail;
    }

    public String getName() {
        return this.userDetail != null ? (this.userDetail instanceof UserDetail ? (UserDetail) this.userDetail : ((JSONObject) this.userDetail).toJavaObject(UserDetail.class)).getName() : null;
    }
}
